package tmproject.hlhj.fhp.cartrans.activitys

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.view.View
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.huantansheng.easyphotos.EasyPhotos
import com.lzy.okgo.model.HttpParams
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.android.synthetic.main.aty_identity.*
import rx.functions.Action1
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.IView.IdentifyView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.customVIews.MyGlideEngin
import tmproject.hlhj.fhp.cartrans.customVIews.SexPop
import tmproject.hlhj.fhp.cartrans.http.Urls
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.DriverInfoBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.presenter.IdentifyPresenter
import tmproject.hlhj.fhp.cartrans.utils.FormatUtil
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils
import java.io.File

/**
 * Created by Administrator on 2018/8/28/028.
 */
class IdentityAty :BaseAty<IdentifyView,IdentifyPresenter>(), IdentifyView, SexPop.OnSexChooseListener {
    private var o1=""//正面路径
    private var ot1=""//正面路径
    private var o2=""//背面路径
    private var ot2=""//背面路径
    private var os=""
    private val Front=122
    private val back=133
    private lateinit var sexDialog:SexPop
    override fun bindView(): IdentifyView {
        return this
    }

    override fun createPresenter(): IdentifyPresenter {
        return IdentifyPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_identity
    }

    override fun setIdentify() {
    }
    override fun initView() {
        sexDialog= SexPop(this,this)


        etName.isFocusable=false
        etName.isFocusableInTouchMode=false

        tvAge.isFocusable=false
        tvAge.isFocusableInTouchMode=false

        tvNum.isFocusable=false
        tvNum.isFocusableInTouchMode=false


        btSave.visibility=View.GONE

        val driver = SpHelper.getDriver(this)
        val infoBean = driver?.data
        etName.setText(infoBean?.name)
        if (infoBean?.sex.equals("M")){
            tvSex.setText("男")
        }else{
            tvSex.setText("女")
        }
        tvAge.setText(infoBean?.age.toString())
        tvNum.setText(infoBean?.id_no)
        o1=infoBean?.id_pic!!
        ot1=infoBean?.id_pic!!
        o2=infoBean?.id_pic_back!!
        ot2=infoBean?.id_pic_back!!
        os=infoBean?.sex
        if (infoBean?.id_pic.contains("uploads")){

            Glide.with(this).load(Urls.ImgBase+infoBean?.id_pic).into(img1)
        }else{
            Glide.with(this).load(infoBean?.id_pic).into(img1)

        }
        if (infoBean?.id_pic_back.contains("uploads")){
            Glide.with(this).load(Urls.ImgBase+infoBean?.id_pic_back).into(img2)
        }else{
            Glide.with(this).load(infoBean?.id_pic_back).into(img2)
        }
        if (!RxPermissions.getInstance(this).isGranted(Manifest.permission.CAMERA)||!RxPermissions.getInstance(this).isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)){
            RxPermissions.getInstance(this).request(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA)
                    .subscribe(object : Action1<Boolean> {
                        override fun call(t: Boolean?) {
                            if (!t!!){
                                finish()
                            }
                        }
                    })
        }
    }

    override fun logicStart() {

    }
    override fun setListener() {
        img1.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(Front)
        }
        img2.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(back)
        }
        tvSex.setOnClickListener {
            sexDialog.show()
        }
        tvSex.isClickable=false
        img1.isClickable=false
        img2.isClickable=false
        btExit.setOnClickListener {
            finish()
        }
        btEdit.setOnClickListener {
            etName.isFocusable=true
            etName.isFocusableInTouchMode=true
            tvAge.isFocusable=true
            tvAge.isFocusableInTouchMode=true

            tvNum.isFocusable=true
            tvNum.isFocusableInTouchMode=true

            img1.isClickable=true
            img2.isClickable=true
            tvSex.isClickable=true

            btSave.visibility=View.VISIBLE

        }
        btSave.setOnClickListener {
            if (etName.text.toString().isEmpty()){
                ToastUtils.toast("姓名不能为空")
                return@setOnClickListener
            }
            if (tvAge.text.toString().isEmpty()){
                ToastUtils.toast("年龄不能为空")
                return@setOnClickListener
            }
            if (!FormatUtil.isIDNumber(tvNum.text.toString())){
                ToastUtils.toast("身份证号格式不正确")
                return@setOnClickListener
            }
            val httpParams = HttpParams()
            httpParams.put("token",SpHelper.getToken(this))
            httpParams.put("type","SM")
            httpParams.put("name",etName.text.toString())
            httpParams.put("sex",os)
            httpParams.put("age",tvAge.text.toString())
            httpParams.put("id_no",tvNum.text.toString())

            if (!o1.equals(ot1)){
                httpParams.put("id_pic",File(ot1))
                log("fhp","上传图片的路径1--${ot1}")
            }
            if (!o2.equals(ot2)){
                httpParams.put("id_pic_back",File(ot2))
                log("fhp","上传图片的路径2--${ot2}")

            }
            presenter?.saveInfo(this,httpParams)
        }
    }

    override fun changeInfo(base: BaseBean) {
        ToastUtils.toast("修改成功")
        etName.isFocusable=false
        etName.isFocusableInTouchMode=false

        tvAge.isFocusable=false
        tvAge.isFocusableInTouchMode=false

        tvNum.isFocusable=false
        tvNum.isFocusableInTouchMode=false

        img1.isClickable=false
        img2.isClickable=false
        btSave.visibility=View.GONE

        val infoBean = SpHelper.getDriver(this)
        val bean = infoBean?.data
        bean?.name=etName.text.toString()
        bean?.age=tvAge.text.toString().toInt()
        bean?.id_no=tvNum.text.toString()
        bean?.id_pic=ot1
        bean?.id_pic_back=ot2
        SpHelper.saveDriver(this,infoBean!!)


        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            Front->{
                ot1=pathList[0]
                Glide.with(this).load(ot1).into(img1)
            }
            back->{
                ot2=pathList[0]
                Glide.with(this).load(ot2).into(img2)

            }
        }
    }

    override fun m() {
        os="M"
        tvSex.text="男"
    }
    override fun f() {
        os="F"
        tvSex.text="女"
    }
}